package pdfdemo;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;

public class PDFDemo {
    public static void main(String[] args) throws Exception {
        createSimpleTable();
    }

    public static void createSimpleTable() throws Exception {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese, 7, Font.BOLD);

        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(new File("C:\\Users\\58212\\Desktop\\测试.pdf")));
        document.open();
        PdfPTable table = new PdfPTable(9);
        table.setWidthPercentage(100);

        PdfPCell shopName = new PdfPCell(centerCell("商品名称",font));
        shopName.setColspan(5);

        PdfPCell good = new PdfPCell(centerCell("雅诗兰黛肌初赋活原生液 400ML",font));
        good.setColspan(5);

        PdfPCell bottom = new PdfPCell(centerCell("商品总计：5件   支付方式：微信支付   合计金额：￥3355.00-折扣金额：￥100=实付金额：￥3255.00",font));
        bottom.setColspan(9);
        // 设置内容水平居中显示
        bottom.setHorizontalAlignment(Element.ALIGN_RIGHT);
        
        table.addCell(centerCell("订单号",font));
        table.addCell(centerCell("提货时间",font));
        table.addCell(centerCell("提货地点",font));
        table.addCell(centerCell("提货人姓名",font));
        table.addCell(centerCell("国籍",font));
        table.addCell(centerCell("证件类型",font));
        table.addCell(centerCell("证件号",font));
        table.addCell(centerCell("航班号",font));
        table.addCell(centerCell("航班时间",font));

        table.addCell(centerCell("GZ20051311060584384548",font));
        table.addCell(centerCell("2020-7-6 12:37:19",font));
        table.addCell(centerCell("广州白云机场T2入境提货点",font));
        table.addCell(centerCell("曹操",font));
        table.addCell(centerCell("中国大陆",font));
        table.addCell(centerCell("护照",font));
        table.addCell(centerCell("E79907999",font));
        table.addCell(centerCell("CZ350",font));
        table.addCell(centerCell("2020-7-6",font));

        table.addCell(centerCell("序号",font));
        table.addCell(centerCell("商品编号",font));
        table.addCell(shopName);
        table.addCell(centerCell("数量",font));
        table.addCell(centerCell("价格",font));

        table.addCell(centerCell("1",font));
        table.addCell(centerCell("C046604",font));
        table.addCell(good);
        table.addCell(centerCell("1",font));
        table.addCell(centerCell("920.00",font));

        table.addCell(bottom);
        document.add(table);
        document.add(new Paragraph("\n\n"));
        document.add(table);
        document.close();
    }

    private static PdfPCell centerCell(String contentStr,Font font,int colSpan){
        Paragraph content = new Paragraph(contentStr, font);
        PdfPCell result = new PdfPCell(content);
        result.setColspan(colSpan);
        result.setHorizontalAlignment(Element.ALIGN_CENTER);
        result.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return result;
    }

    private static PdfPCell centerCell(String contentStr,Font font){
        return centerCell(contentStr, font,1);
    }

}
